package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo10MapValues {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
      .setMaster("local")
      .setAppName("MapValues算子演示")

    val sc: SparkContext = new SparkContext(conf)

    val linesRDD: RDD[String] = sc.textFile("spark/data/students.txt")

    val kvRDD1: RDD[(String, Int)] = linesRDD.map(_.split(",")).map {
      case Array(_, name:String, age:String, _,_) =>
        (name, age.toInt)
    }


    /**
     * mapValues函数也是作用在kv格式的算子上
     * 将每个元素的值传递给后面的函数，进行处理得到新的值，键不变，这个处理后的组合重新返回到新的RDD中
     */
    kvRDD1.mapValues(_ + 100).foreach(println)

  }
}
